﻿using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using LBoL.Base;
using LBoL.Base.Extensions;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Battle.Interactions;
using LBoL.Core.Cards;
using LBoL.Core.StatusEffects;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;
using ProbeMod.StatusEffects;

namespace ProbeMod.Cards
{
    public sealed class ProbeTacticalSelectionDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            var config = GetCardDefaultConfig();

            config.Rarity = Rarity.Common;
            config.Type = CardType.Skill;
            config.TargetType = TargetType.Nobody;

            config.Colors = new List<ManaColor>() { ManaColor.Blue };
            config.Cost = new ManaGroup() { Blue = 1 };
            config.UpgradedCost = new ManaGroup() { Blue = 1 };

            config.Value1 = 3;  //  抽牌数
            config.Value2 = 1;  //  弃牌数

            config.UpgradedKeywords = Keyword.Debut;

            config.Index = CardIndexGenerator.GetUniqueIndex(config);

            return config;
        }
    }

    /// <summary>
    /// 战术选择
    /// 抉择：随机弃1张费用最低的牌，从抽牌堆中抽2张费用最高的牌；
    /// 随机弃1张费用最高的牌，从抽牌堆中抽2张费用最低的牌；
    /// 初见：将以上两个选项全执行一遍！
    /// </summary>
    [EntityLogic(typeof(ProbeTacticalSelectionDef))]
    public sealed class ProbeTacticalSelection : ProbeCard
    {
        bool inited;

        protected override void OnEnterBattle(BattleController battle)
        {
            base.OnEnterBattle(battle);
            inited = false;
        }

        protected override string GetBaseDescription()
        {
            if (base.DebutActive || !IsUpgraded)
            {
                return base.GetBaseDescription();
            }
            return base.GetExtraDescription3;
        }

        public override Interaction Precondition()
        {

            List<ProbeTacticalSelection> list = Library.CreateCards<ProbeTacticalSelection>(2, IsUpgraded).ToList();
            var consider = list[0];
            var consider2 = list[1];
            consider.ChoiceCardIndicator = 1;
            consider2.ChoiceCardIndicator = 2;
            consider.SetBattle(base.Battle);
            consider2.SetBattle(base.Battle);

            return !inited && IsUpgraded?null: new MiniSelectCardInteraction(list);
        }

        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            if (base.TriggeredAnyhow)
            {
                // 选项1
                if (Battle.HandZone.Count > 0)
                {
                    // 获取手牌中最小Mana
                    var discardMana = Battle.HandZone.Min(c => SynergyAmount(c.Cost, ManaColor.Any));
                    //  获取所有此Mana的手牌
                    var discardList = Battle.HandZone.Where(c => SynergyAmount(c.Cost, ManaColor.Any) == discardMana);
                    yield return new DiscardAction(discardList.Sample(GameRun.BattleRng));
                }
                for (var i = 0; i < Value1; i++)
                {
                    if (Battle.DrawZone.Count > 0)
                    {
                        var drawMana = Battle.DrawZone.Max(c => SynergyAmount(c.Cost, ManaColor.Any));
                        //  获取所有此Mana的手牌
                        var drawList = Battle.DrawZone.Where(c => SynergyAmount(c.Cost, ManaColor.Any) == drawMana);
                        yield return new DrawSelectedCardAction(drawList.Sample(GameRun.BattleRng));
                    }
                }

                //  选项2
                if (Battle.HandZone.Count > 0)
                {
                    // 获取手牌中最小Mana
                    var discardMana = Battle.HandZone.Max(c => SynergyAmount(c.Cost, ManaColor.Any));
                    //  获取所有此Mana的手牌
                    var discardList = Battle.HandZone.Where(c => SynergyAmount(c.Cost, ManaColor.Any) == discardMana);
                    yield return new DiscardAction(discardList.Sample(GameRun.BattleRng));
                }
                for (var i = 0; i < Value1; i++)
                {
                    if (Battle.DrawZone.Count > 0)
                    {
                        var drawMana = Battle.DrawZone.Min(c => SynergyAmount(c.Cost, ManaColor.Any));
                        //  获取所有此Mana的手牌
                        var drawList = Battle.DrawZone.Where(c => SynergyAmount(c.Cost, ManaColor.Any) == drawMana);
                        yield return new DrawSelectedCardAction(drawList.Sample(GameRun.BattleRng));
                    }
                }
                inited = true;
            }

            var card = ((MiniSelectCardInteraction)precondition)?.SelectedCard;
            if (card != null)
            {

                if (card.ChoiceCardIndicator == 1)
                {
                    // 选项1
                    if (Battle.HandZone.Count > 0)
                    {
                        // 获取手牌中最小Mana
                        var discardMana = Battle.HandZone.Min(c => SynergyAmount(c.Cost, ManaColor.Any));
                        //  获取所有此Mana的手牌
                        var discardList = Battle.HandZone.Where(c => SynergyAmount(c.Cost, ManaColor.Any) == discardMana);
                        yield return new DiscardAction(discardList.Sample(GameRun.BattleRng));
                    }
                    for (var i = 0; i < Value1; i++)
                    {
                        if(Battle.DrawZone.Count > 0)
                        {
                            var drawMana = Battle.DrawZone.Max(c => SynergyAmount(c.Cost, ManaColor.Any));
                            //  获取所有此Mana的手牌
                            var drawList = Battle.DrawZone.Where(c => SynergyAmount(c.Cost, ManaColor.Any) == drawMana);
                            yield return new DrawSelectedCardAction(drawList.Sample(GameRun.BattleRng));
                        }
                    }
                }
                else
                {
                    //  选项2
                    if (Battle.HandZone.Count > 0)
                    {
                        // 获取手牌中最小Mana
                        var discardMana = Battle.HandZone.Max(c => SynergyAmount(c.Cost, ManaColor.Any));
                        //  获取所有此Mana的手牌
                        var discardList = Battle.HandZone.Where(c => SynergyAmount(c.Cost, ManaColor.Any) == discardMana);
                        yield return new DiscardAction(discardList.Sample(GameRun.BattleRng));
                    }
                    for (var i = 0; i < card.Value1; i++)
                    {
                        if (Battle.DrawZone.Count > 0)
                        {
                            var drawMana = Battle.DrawZone.Min(c => SynergyAmount(c.Cost, ManaColor.Any));
                            //  获取所有此Mana的手牌
                            var drawList = Battle.DrawZone.Where(c => SynergyAmount(c.Cost, ManaColor.Any) == drawMana);
                            yield return new DrawSelectedCardAction(drawList.Sample(GameRun.BattleRng));
                        }
                    }
                }
            }


        }

    }
}
